package com.novellotus.iov.carvice.maps;

import com.amap.api.maps.AMap;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.animation.AlphaAnimation;
import com.amap.api.maps.model.animation.Animation;
import com.amap.api.services.core.PoiItem;
import com.novellotus.iov.carvice.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Poi图层类。在高德地图API里，如果要显示Poi，可以用此类来创建Poi图层。如不满足需求，也可以自己创建自定义的Poi图层。
 *
 * @since V2.1.0
 */
public class PoiOverlay {

    private AMap mAMap;
    private ArrayList<Marker> mPoiMarks = new ArrayList<>();

    /**
     * 通过此构造函数创建Poi图层。
     *
     * @param amap 地图对象。
     * @param pois 要在地图上添加的poi。列表中的poi对象详见搜索服务模块的基础核心包（com.amap.api.services.core）中的类<strong> <a href="../../../../../../Search/com/amap/api/services/core/PoiItem.html" title="com.amap.api.services.core中的类">PoiItem</a></strong>。
     * @since V2.1.0
     */
    public PoiOverlay(AMap amap, List<PoiItem> pois) {
        mAMap = amap;
        addClusterToMap(pois);
    }


    /**
     * @param poiItem 设置充电站marker的位置、名称和地址
     * @return
     */
    private MarkerOptions getMarkerOptions(PoiItem poiItem) {
        return new MarkerOptions()
                .position(new LatLng(poiItem.getLatLonPoint()
                        .getLatitude(), poiItem
                        .getLatLonPoint().getLongitude()))
                .title(poiItem.getTitle()).snippet(poiItem.getSnippet())
                .icon(getBitmapDescriptor(poiItem)).infoWindowEnable(false);
    }


    /**
     * 给第几个Marker设置图标，并返回更换图标的图片。如不用默认图片，需要重写此方法。
     *
     * @return 更换的Marker图片。
     * @since V2.1.0
     */
    protected BitmapDescriptor getBitmapDescriptor(PoiItem poiItem) {
        return BitmapDescriptorFactory.fromResource(R.mipmap.icon_mine_charge);
    }

    /**
     * marker渐变动画，动画结束后将Marker删除
     */
    private class MyAnimationListener implements Animation.AnimationListener {
        private List<Marker> mRemoveMarkers;

        MyAnimationListener(List<Marker> removeMarkers) {
            mRemoveMarkers = removeMarkers;
        }

        @Override
        public void onAnimationStart() {
        }

        @Override
        public void onAnimationEnd() {
            for (Marker marker : mRemoveMarkers) {
                marker.remove();
            }
            mRemoveMarkers.clear();
            mRemoveMarkers=null;
        }
    }

    /**
     * 将聚充电站添加至地图上
     */
    private AlphaAnimation mADDAnimation = new AlphaAnimation(0, 1);

    public void addClusterToMap(List<PoiItem> pois) {

        ArrayList<Marker> removeMarkers = new ArrayList<>();
        removeMarkers.addAll(mPoiMarks);
        mPoiMarks.clear();
        AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
        MyAnimationListener myAnimationListener = new MyAnimationListener(removeMarkers);
        for (Marker marker : removeMarkers) {
            marker.setAnimation(alphaAnimation);
            marker.setAnimationListener(myAnimationListener);
            marker.startAnimation();
        }

        for (PoiItem poiItem : pois) {
            addSingleClusterToMap(poiItem);
        }
    }


    /**
     * @param poiItem 添加充电站
     */
    private void addSingleClusterToMap(PoiItem poiItem) {
        Marker marker = mAMap.addMarker(getMarkerOptions(poiItem));
        marker.setAnimation(mADDAnimation);
        marker.startAnimation();
        mPoiMarks.add(marker);
    }

    public void  clear(){
        for(Marker marker:mPoiMarks){
            marker.destroy();
        }
        mPoiMarks.clear();
        mPoiMarks=null;
        mAMap=null;
    }
}
